/**
 * @file: app.c
 * @brief: APP层
 * @author: liaowx
 * @date: 2019.07.30
 */
#include <stdio.h>
#include <malloc.h>

#include "ioctl.h"
#include "app.h"
#include "device.h"

static void app_open(int cmd, int arg, struct core *core);

int main()
{
    struct app *app = (struct app *)malloc(sizeof(struct app));
    struct core *sort_core;
    int cmd;

    /*来自APP的信息*/
    app->cmd = BUBBLE;
    app->arg = 5;
    app->core = (struct core *)malloc(sizeof(struct core));
    app->app_open = app_open;

    /* 填充app中core的信息 */
    app->app_open(app->cmd, app->arg, app->core);

    /* 向中间层拿去底层信息 */
    sort_core = sort_ioctl(GET_DEV_CTL, NULL);

    /* 匹配APP层与底层 */
    cmd = match(app->core, sort_core);
    if (cmd == FALSE) {
        printf("没有匹配项\n");
        return -1;
    }

    sort_core = &sort_core[cmd];
    sort_core->ioctl(DEV_CTL, sort_core);

    return 0;
}

static void app_open(int cmd, int arg, struct core *core)
{
    core->cmd = cmd;
    core->arg = arg;
}